<?php 
	include('function/connection.php');
	include('function/function.php');
	
	if(isset($_GET['action']) && $_GET['action'] == "oustanding"){
		$customer = $_POST['txtCustomer'];
		$amount = $_POST['txtAmount'];
		$paid = $_POST['txtPaid'];
		$balance = $_POST['txtBalance'];
		$date=$_POST['txtDate'];		
		$selected_date=$_POST['txtOustDate'];
		$selected_date=strtotime( $selected_date );
		$mysqldate = date( 'Y-m-d', $selected_date );	
		$bill = $_POST['txtBill'];	
		$newPay = $_POST['txtNewPay'];
		$leftMoney=$_POST['txtleftMoney'];
		
		$newBalance = (int)$balance - (int)$newPay;
		$newPaid = (int)$paid + (int)$newPay;
		$id = $_GET['bill'];
		//insert value to table owe
		$sql = "INSERT INTO tbl_owes (type,cus_name,amount, paid, balance, date,next_date, owe_numer)
		VALUES(
			'sales',
			'$customer',
			'$amount',
			'$newPay',
			'$newBalance',
			'$date',
			'$mysqldate',
			'$bill')";
			
		 $result = mysql_query($sql);
		 //update paid and balance in table sales
		 if($result){
			$sql="UPDATE tbl_sales SET paid = '$newPaid', balance = '$newBalance' WHERE billnumber = '$id'";
		 //get max id from tbl_owe to print report
			if(mysql_query($sql)){
				$max =maxOfAll1("owe_id	", "tbl_owes");	
			    echo "<script>window.open('sale_print_payment.php?owe_id=$max&leftMoney=$leftMoney','myNewWinsr','width=620,height=800,toolbar=0,menubar=no,status=no,resizable=yes,location=no,directories=no');</script>";
			}
		 }
		pageRedirect("index.php?page=oustanding_list&action=oustanding_list&","Updated balance successful...!");
	}
	
	if(isset($_GET['action']) && $_GET['action'] == "pending"){	
		$supplier = $_POST['txtSupplier'];
		$amount = $_POST['txtAmount'];
		$paid = $_POST['txtPaid'];
		$balance = $_POST['txtBalance'];
		$date=$_POST['txtDate'];		
		$selected_date=$_POST['txtPendingDate'];
		$selected_date=strtotime( $selected_date );
		$mysqldate = date( 'Y-m-d', $selected_date );	
		$bill = $_POST['txtBill'];	
		$newPay = $_POST['txtNewPay'];
		
		$id = $_GET['bill'];
		$newBalance = $balance - $newPay;
		$newPaid = $paid + $newPay;
			
		$sql="INSERT INTO tbl_owes (type,sup_name,amount, paid, balance, date,next_date, owe_numer)
		VALUES(
			'entry',
			'$supplier',
			'$amount',
			'$newPay',
			'$newBalance',
			'$date',
			'$mysqldate',
			'$bill')";
			
		 $result=mysql_query($sql);
		 if($result){
			$sql="UPDATE tbl_stocks SET paid = '$newPaid', balance = '$newBalance' WHERE billnumber = '$id'";
			mysql_query($sql);
		 }
		pageRedirect("index.php?page=pending_list&action=pending_list&","Updated balance successful...!");
	}
?>